package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.job.utils.HttpGetPostUtils;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.BUserInfoService;
import io.lkt.modules.sys.service.BUserPromotionSettleInfoService;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 推广结算信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2019-04-13 09:39:25
 */
@RestController
@RequestMapping("/sys/buserpromotionsettleinfo")
public class BUserPromotionSettleInfoController extends AbstractController {
    @Autowired
    private BUserPromotionSettleInfoService bUserPromotionSettleInfoService;

    @Autowired
    private BUserInfoService bUserInfoService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Value("${serviceUrls}")
    private String serviceUrls;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:buserpromotionsettleinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = bUserPromotionSettleInfoService.queryPage(params);

        SysUserEntity curUser= getUser();
        //params.put("curUserId",curUser.getUserId());

        List<SysUserGzhEntity> userGzhList = this.sysUserRoleService.queryGzhList(curUser.getUserId());
        params.put("userGzhList", userGzhList);
        Query query = new Query(params);

        List<BUserPromotionSettleInfoEntity> list=bUserPromotionSettleInfoService.queryList(query);
        Integer total=bUserPromotionSettleInfoService.queryTotal(query);
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:buserpromotionsettleinfo:info")
    public R info(@PathVariable("id") Integer id)throws Exception{
        BUserPromotionSettleInfoEntity bUserPromotionSettleInfo = bUserPromotionSettleInfoService.getById(id);

        BUserInfoEntity bUserInfoEntity= bUserInfoService.getById(bUserPromotionSettleInfo.getUserId());
        if(bUserInfoEntity!=null){
            bUserPromotionSettleInfo.setAccountBalance(bUserInfoEntity.getAccountBalance());
        }

        return R.ok().put("bUserPromotionSettleInfo", bUserPromotionSettleInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:buserpromotionsettleinfo:save")
    public R save(@RequestBody BUserPromotionSettleInfoEntity bUserPromotionSettleInfo){
        bUserPromotionSettleInfo.setCreateTime(new Date());
        bUserPromotionSettleInfoService.save(bUserPromotionSettleInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:buserpromotionsettleinfo:update")
    public R update(@RequestBody BUserPromotionSettleInfoEntity bUserPromotionSettleInfo) throws IOException {
        ValidatorUtils.validateEntity(bUserPromotionSettleInfo);
        bUserPromotionSettleInfo.setModifyTime(new Date());
        bUserPromotionSettleInfoService.updateById(bUserPromotionSettleInfo);

        BUserInfoEntity userInfoEntity=new BUserInfoEntity();
        userInfoEntity.setUserId(bUserPromotionSettleInfo.getUserId());
        userInfoEntity.setAccountBalance(bUserPromotionSettleInfo.getAccountBalance());
        bUserInfoService.updateById(userInfoEntity);

        HttpGetPostUtils.delCache(serviceUrls,"UserInfoMapper");

//        HttpClient httpClient = HttpClientBuilder.create().build();
//        HttpGet get = new HttpGet("https://service.rrdsp.cn/cache/delCache?key=com.tulele.vtool.mapper.UserInfoMapper");
//        httpClient.execute(get);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:buserpromotionsettleinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        bUserPromotionSettleInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    public final static String MD5_KEY = "8098jjjsdufpsdiklwetfafa";

    public  String md5Encode(String text) throws Exception {
        //加密后的字符串
        String encodeStr= DigestUtils.md5Hex(text + MD5_KEY);
        System.out.println("[encodeStr] = " + encodeStr);
        return encodeStr;
    }

    @RequestMapping("/tuiguang/{id}")
    public R tuiguang(@PathVariable("id") Integer id)throws Exception{
        String openId = bUserInfoService.getById(id).getOpenId();
        String sessionId=md5Encode(id.toString()+openId);
        return R.ok().put("sessionId",sessionId);
    }

}
